import os
import re

def rename_fruit_images():
    # 获取当前目录
    current_dir = os.getcwd()
    
    # 遍历当前目录下的所有文件
    for filename in os.listdir(current_dir):
        # 使用正则表达式匹配"生成水果图片(n)"格式的文件名
        match = re.search(r'生成水果图片\s*\((\d+)\)', filename)
        if match:
            # 提取数字部分
            number = match.group(1)
            # 构建新文件名
            new_filename = re.sub(r'生成水果图片\s*\(\d+\)', f'fruits({number})', filename)
            # 完整路径
            old_path = os.path.join(current_dir, filename)
            new_path = os.path.join(current_dir, new_filename)
            
            try:
                # 重命名文件
                os.rename(old_path, new_path)
                print(f"已重命名: {filename} -> {new_filename}")
            except Exception as e:
                print(f"无法重命名 {filename}: {e}")

if __name__ == "__main__":
    rename_fruit_images()    